package what.basic.UImanager;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;

import what.basic.basiccomponent.*;
public class JuserUIManager {
	JLabel lbl;
	String nodename=null;
	JPanel p1;
	JPanel p2;
	JPanel p3;
	JuserUIManager(){
		JFrame f=new Jframe().jframe("what to buy", 700, 400, true);
		p1=new JPanel();
		p2=new JPanel();
		p3=new JPanel();
		lbl=new JLabel("data change to:");
		JSplitPane stp=new JSplitPane();
		stp.setOneTouchExpandable(true);
		stp.setContinuousLayout(true);
		stp.setPreferredSize(new Dimension(100,200));
		stp.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		stp.setLeftComponent(p1);
		stp.setRightComponent(p2);
		stp.setDividerSize(3);
		final lookandfeel1 lf=new lookandfeel1();
		JScrollPane sp=new JScrollPane();
		sp.setViewportView(lf.tree);
		JSplitPane stp1=new JSplitPane();
		stp1.setOneTouchExpandable(true);
		stp1.setContinuousLayout(true);
		stp1.setOrientation(JSplitPane.VERTICAL_SPLIT);
		stp1.setPreferredSize(new Dimension(100,200));
		stp1.setTopComponent(sp);
		stp1.setBottomComponent(lbl);
		stp1.setDividerSize(1);
		JButton b1=new JButton("add node");
		JButton b2=new JButton("delete node");
		p3.setLayout(new FlowLayout());
		p3.add(b1);
		p3.add(b2);
		JSplitPane stp2=new JSplitPane();
		stp2.setOneTouchExpandable(true);
		stp2.setContinuousLayout(true);
		stp2.setPreferredSize(new Dimension(100,200));
		stp2.setOrientation(JSplitPane.VERTICAL_SPLIT);
		lookandfeel2 lf2=new lookandfeel2();
		stp2.setTopComponent(lf2.tp);
		stp2.setBottomComponent(p3);
		stp2.setDividerSize(1);
		stp2.setDividerLocation(80);
		p1.setLayout(new GridLayout(1,1));
		p1.add(stp1);
		p2.setLayout(new GridLayout(1,1));
		p2.add(stp2);
		f.setContentPane(stp);
		f.pack();
	}
	public static void main(String[] args) {
		new JuserUIManager();
	}
}
class lookandfeel1 implements TreeModelListener {
		JLabel lbl;
		String nodeName=null;
		DefaultMutableTreeNode root;
		DefaultMutableTreeNode node1;
		DefaultMutableTreeNode node2;
		DefaultMutableTreeNode node3;
		DefaultMutableTreeNode node4;
		JTree tree;
		DefaultTreeModel treemodel;
		lookandfeel1(){
			try{
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
			catch(Exception e){}
			JFrame f=new Jframe().jframe("defaultmutabletreenode", 300, 00, true);
			Container cp=f.getContentPane();
			 root=new DefaultMutableTreeNode("today to buy");
			 node1=new DefaultMutableTreeNode("vegetable");
			node2=new DefaultMutableTreeNode("fruit");
			node3=new DefaultMutableTreeNode("present");
			node4=new DefaultMutableTreeNode("small househode items");
			root.add(node1);
			root.add(node2);
			root.add(node3);
			root.add(node4);
			DefaultMutableTreeNode leafnode=new DefaultMutableTreeNode("baicai");
			DefaultMutableTreeNode lnode=new DefaultMutableTreeNode("xiaobaicai");
			node1.add(leafnode);
			leafnode.add(lnode);
			tree=new JTree(root);
			tree.setEditable(true);
			tree.addMouseListener(new MouseHandle());
			tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
			final DefaultTreeModel treemodel=(DefaultTreeModel)tree.getModel();
			treemodel.addTreeModelListener(this);
			lbl=new JLabel("change data to:");
			JButton b1=new JButton("add node");
			JButton b2=new JButton ("delete node");
			Container con=new Container();
			con.setLayout(new FlowLayout());
			con.add(b1);
			con.add(b2);
			con.add(lbl);
			JScrollPane sp=new JScrollPane();
			sp.setViewportView(tree);
			cp.add(sp,BorderLayout.CENTER);
			cp.add(con,BorderLayout.SOUTH);
			f.pack();
			b1.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent ev){
					DefaultMutableTreeNode pn=null;
					DefaultMutableTreeNode newnode=new DefaultMutableTreeNode("new node");
					newnode.setAllowsChildren(true);
					TreePath ppath=tree.getSelectionPath();
					pn=(DefaultMutableTreeNode)(ppath.getLastPathComponent());
					treemodel.insertNodeInto(newnode, pn, pn.getChildCount());
					tree.scrollPathToVisible(new TreePath(newnode.getPath()));
					lbl.setText("add new node successed.");
				}
			});
			b2.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent eve){
					TreePath treepath=tree.getSelectionPath();
					if(treepath!=null){
						DefaultMutableTreeNode sn=(DefaultMutableTreeNode)treepath.getLastPathComponent();
						TreeNode parent=(TreeNode)sn.getParent();
						if(parent!=null){
							treemodel.removeNodeFromParent(sn);
							lbl.setText("delete node successed!");
						}
					}
				}
			});
			tree.addTreeSelectionListener(new TreeSelectionListener(){

				@Override
				public void valueChanged(TreeSelectionEvent e) {
					DefaultMutableTreeNode node=(DefaultMutableTreeNode)e.getPath().getLastPathComponent();
					lbl.setText("you selected:"+node);
				}

						
			});
		}
		class MouseHandle extends MouseAdapter{
		public void mousePressed(MouseEvent e){
			try{
				JTree tree=(JTree)e.getSource();
				int rowLocation=tree.getRowForLocation(e.getX(), e.getY());
				TreePath treepath=tree.getPathForRow(rowLocation);
				TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
				nodeName=treenode.toString();
			}
			catch(NullPointerException ne){}
		}
		}
		
		public void treeNodesChanged(TreeModelEvent e) {
			TreePath treepath=e.getTreePath();
			DefaultMutableTreeNode node=(DefaultMutableTreeNode)treepath.getLastPathComponent();
			try{
				int[] index=e.getChildIndices();
				node=(DefaultMutableTreeNode)node.getChildAt(0);
			}
			catch(NullPointerException exc){		}
			lbl.setText(nodeName+" change to :" +(String)node.getUserObject());
		}
		public void treeNodesInserted(TreeModelEvent e) {	}
		public void treeNodesRemoved(TreeModelEvent e) {	}
		public void treeStructureChanged(TreeModelEvent e) {	}
}
class lookandfeel2{
	JPanel p1;
	JPanel p2;
	JPanel p3;
	JPanel p4;
	JPanel p5;
	static JTextArea ta1;
	static JTextArea ta2;
	static JTextArea ta3;
	static JTextArea ta4;
	static JTextArea ta5;
	static JTabbedPane tp;
	lookandfeel2(){
		try{
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		}
		catch(Exception e){}
		ta1=new JTextArea(30,30);
		ta2=new JTextArea(40,40);
		ta3=new JTextArea(40,40);
		ta4=new JTextArea(40,40);
		ta5=new JTextArea(40,40);
		tp=new JTabbedPane();
		p1=new JPanel();
		p2=new JPanel();
		p3=new JPanel();
		p4=new JPanel();
		p5=new JPanel();
		tp.addTab("p1", p1);
		tp.setEnabledAt(0, true);
		tp.setTitleAt(0, "introduction");
		tp.addTab("p2", p2);
		tp.setEnabledAt(1, true);
		tp.setTitleAt(1, "usage");
		tp.addTab("p3", p3);
		tp.setEnabledAt(2, true);
		tp.setTitleAt(2, "gedibijiao");
		tp.addTab("p4", p4);
		tp.setEnabledAt(3, true);
		tp.setTitleAt(3, "jinguolaiyuan");
		tp.addTab("p5", p5);
		tp.setEnabledAt(4, true);
		tp.setTitleAt(4, "memo");
		tp.setPreferredSize(new Dimension(500,200));
		tp.setTabPlacement(JTabbedPane.TOP);
		tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		p1.setLayout(new FlowLayout());
		p1.add(ta1);
		p2.setLayout(new FlowLayout());
		p2.add(ta2);
		p3.setLayout(new FlowLayout());
		p3.add(ta3);
		p4.setLayout(new FlowLayout());
		p4.add(ta4);
		p5.setLayout(new FlowLayout());
		p5.add(ta5);
		
	}
	
}
